package top.wyloop.fasthome.module.system.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.wyloop.fasthome.logger.ControllerLog;
import top.wyloop.fasthome.module.system.model.entity.Home;
import top.wyloop.fasthome.module.system.service.HomeService;
import top.wyloop.fasthome.result.RetResult;

/**
 * @author Wyatt6
 * @date 2025/5/9
 */
@Slf4j
@RestController
@RequestMapping("/api/system/home")
public class HomeController {
    @Autowired
    private HomeService homeService;

    @ControllerLog("创建家庭API")
    @SaCheckLogin
    @PostMapping("/createHome")
    public RetResult createHome(@RequestBody Home input) {
        Assert.notNull(input, "输入数据为空");
        Assert.hasText(input.getName(), "家庭名称为空");
        Assert.hasText(input.getPassword(), "口令为空");

        String name = input.getName();
        String password = input.getPassword();
        log.info("请求参数: name=[{}]", name);

        Home home = homeService.createHome(StpUtil.getLoginIdAsLong(), name.trim(), password.trim());
        return new RetResult().success().addData("home", home);
    }

    @ControllerLog("更换口令API")
    @SaCheckLogin
    @PostMapping("/changePassword")
    public RetResult changePassword(@RequestBody Home input) {
        Assert.notNull(input, "输入数据为空");
        Assert.hasText(input.getPassword(), "新口令为空");

        homeService.changePassword(StpUtil.getLoginIdAsLong(), input.getPassword().trim());
        return new RetResult().success();
    }
}
